package com.daiqee.controller;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.daiqee.pojo.GoodsList;
import com.daiqee.pojo.StockIn;
import com.daiqee.controller.support.BaseController;
import com.daiqee.enums.RequestType;
import com.daiqee.service.impl.GoodsListService;
import com.daiqee.service.impl.StockInService;
import com.daiqee.vo.BootstrapTablePageVO;
import com.daiqee.vo.JsonObjsVO;
import com.daiqee.util.MyBatisPlusUtils;
import com.daiqee.util.WarpperStringUtils;

/**
 * 入库
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V2.1
 * @author RichardTang 
 * @date: 2018年5月7日 下午5:55:55
 * @package com.daiqee.handler
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
@Controller
@RequestMapping("repository/stock-in")
public class StockInController extends BaseController{
	
	@Autowired
	private StockInService stockInService;
	
	@Autowired
	private GoodsListService goodsListService;
	
	/**
	 * 生成分页条件对象返回BootstrapTablePageVO
	 */
	@SuppressWarnings("all")
	public BootstrapTablePageVO generatorBootVO(Integer offset,Integer limit,EntityWrapper ew,ServiceImpl service) {
		Page page = new Page(offset,limit);
		Page pageData = service.selectPage(page,ew);
		return new BootstrapTablePageVO(pageData.getPages(),pageData.getTotal(),pageData.getRecords());
	}
	
	/**
	 * 查询 入库信息 (如果有传值则根据对象的值来做条件查询)
	 * @return restful风格 所有的入库信息
	 */
	@RequestMapping(method=RequestMethod.GET)
	public Object query(StockIn stockIn,String dateScope,Integer limit,Integer offset){
		EntityWrapper<StockIn> ew = 
		    MyBatisPlusUtils.generatorEntityWrapperByEntityAndDateScope(stockIn, dateScope);
		return restfulResponse.response(generatorBootVO(offset,limit,ew,stockInService),RequestType.GET);
	}

	/**
	 * 查询 指定的入库单下的 对应货物
	 * @return restful风格 所有的入库信息
	 */
	@RequestMapping(value = "/goods-list",method=RequestMethod.GET)
	public Object queryById(StockIn stockIn,
			@RequestParam(name = "limit" , defaultValue = "10")Integer limit,
			@RequestParam(name = "limit" , defaultValue = "0")Integer offset){
		EntityWrapper<GoodsList> ew = new EntityWrapper<GoodsList>();
		ew.eq("goods_list_in_stock_id", stockIn.getStockInId());
		return restfulResponse.response(generatorBootVO(offset,limit,ew,goodsListService),RequestType.GET);
	}
	
	/**
	 * 添加 入库信息
	 * 
	 * @param jsonObjs
	 * 			DTO对象,传递过来的Json必须包含array和obj两个key
	 * 
	 * @return restful 风格 Http状态码
	 */
	@RequestMapping(method=RequestMethod.POST)
	public Object add(@RequestBody JsonObjsVO<GoodsList,StockIn> jsonObjs){
		isNormalFinish = stockInService.addGoodsListAndStockIn(jsonObjs.getArray(),jsonObjs.getObj());
		return restfulResponse.response(isNormalFinish,RequestType.POST);
	}
	
	/**
	 * 删除 入库单
	 * 
	 * @param  ids
	 * 			  需要删除的记录的id号
	 * 
	 * @return restful风格 Http状态码 Void
	 */
	@RequestMapping(value="/{ids}",method=RequestMethod.DELETE)
	public Object remove(@PathVariable("ids") String ids){
		GoodsList goods = new GoodsList();
		//设置货物 是否存在入库单标记
		goods.setGoodsListInFlag("N");
		goods.setGoodsListOperatLog("该货物对应的入库单数据在:"+
	        new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(new Date())+"删除-操作用户:"+getCurrentUser().getUseAuthor()
		);
		List<String> idList = WarpperStringUtils.getIdListByCommaSplit(ids);
		//查询后台数据库
		isNormalFinish = stockInService.removeStockInOrder(idList,goods);
		return restfulResponse.response(isNormalFinish,RequestType.DELETE);
	}

	/**
	 * 更新 入库信息
	 * 
	 * @param jsonObjs
	 * 			DTO对象,传递过来的Json必须包含array和obj两个key
	 * 
	 * @return restful 风格 Http状态码
	 * @throws Exception 
	 */
	@RequestMapping(method=RequestMethod.PUT)
	public Object update(@RequestBody JsonObjsVO<GoodsList,StockIn> jsonObjs) throws Exception{
		List<String> removeIds = WarpperStringUtils.getIdListByCommaSplit(jsonObjs.getIds());
		isNormalFinish = stockInService.updateGoodsListAndStockIn(jsonObjs.getArray(),
		    jsonObjs.getObj(),removeIds,getCurrentUser().getUseAuthor());
		return restfulResponse.response(isNormalFinish,RequestType.PUT);
	}
	
}
